package by.bucha.testproject.xml.handlers;

import org.xml.sax.Attributes;

import by.bucha.testproject.xml.XmlParser.IXmlHandler;
import by.bucha.testproject.xml.bo.QuoteModel;
import by.bucha.testproject.xml.bo.ResultModel;

public class ResultHandler implements IXmlHandler<ResultModel> {

	private static final String TAG_TOTAL_PAGES = "totalPages";

	private static final String TAG_RESULT = "result";

	private static final String TAG_QUOTE = "quote";

	private static final String TAG_ID = "id";

	private static final String TAG_DATE = "date";

	private static final String TAG_TEXT = "text";

	private ResultModel mResult;

	private QuoteModel mCurrentQuote;

	public ResultHandler() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void startDocument() {
		// TODO Auto-generated method stub

	}

	@Override
	public void startTag(String tagName, Attributes atts) {
		if (tagName.equalsIgnoreCase(TAG_RESULT)) {
			mResult = new ResultModel();
		} else if (tagName.equalsIgnoreCase(TAG_QUOTE)) {
			mCurrentQuote = new QuoteModel();
		}
	}

	@Override
	public void endTag(String tagName, String value) {
		if (mResult != null) {
			if (tagName.equalsIgnoreCase(TAG_TOTAL_PAGES)) {
				mResult.setTotalPages(value);
			} else if (tagName.equalsIgnoreCase(TAG_QUOTE)) {
				mResult.addQuoteModel(mCurrentQuote);
				mCurrentQuote = null;
			} else if (tagName.equalsIgnoreCase(TAG_ID)) {
				mCurrentQuote.setId(value);
			} else if (tagName.equalsIgnoreCase(TAG_DATE)) {
				mCurrentQuote.setDate(value);
			} else if (tagName.equalsIgnoreCase(TAG_TEXT)) {
				mCurrentQuote.setText(value);
			}
		}
	}

	@Override
	public ResultModel getResult() {
		return mResult;
	}

}
